<template>
    <div class="createProcedure" v-loading="loading" element-loading-text="加载中"
            element-loading-background="rgba(250, 250, 250, 0.5)" element-loading-spinner="el-icon-loading">
        <div class="title">{{ title }}</div>
        <div class="formArea">
            <el-form :model="createForm" :rules="rules" ref="createForm" label-width="120px" size="mini" :hide-required-asterisk="true">
                <el-form-item label="工序编号">
                    <el-input v-model="createForm.code" placeholder="请输入,忽略将自动生成" :disabled='disabled' :maxlength="120"></el-input>
                </el-form-item>
                <el-form-item label="工序名称" prop="name">
                    <template slot='label'>
                        <span style="color: red">*</span>
                        工序名称
                    </template>
                    <el-input v-model="createForm.name" placeholder="请填写" :maxlength="120"></el-input>
                </el-form-item>
                <el-form-item label="报工权限" prop="permissionsName">
                    <template slot='label'>
                        <span style="color: red">*</span>
                        报工权限
                    </template>
                    <el-select v-model="createForm.permissionsName" multiple @visible-change='treeChange' @remove-tag='tabRemove'>
                        <el-option :value="[]" style="height: auto">
                            <el-tree :data="process.options" show-checkbox node-key="id" :props="defaultProps"
                                :expand-on-click-node='false' ref="roleTree" check-strictly
                                @check="handleNodeCheck">
                            </el-tree>
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="报工数配比" prop="rate">
                    <template slot='label'>
                        <span style="color: red">*</span>
                        报工数配比
                    </template>
                    <el-input-number v-model="createForm.rate" controls-position="right" :min="1" placeholder="请填写">
                    </el-input-number>
                </el-form-item>
                <el-form-item label="不良品项列表" prop="badProductName">
                    <template slot='label'>
                        <span style="color: red">*</span>
                        不良品项列表
                    </template>
                    <el-select
                        v-model="createForm.badProductName"
                        multiple
                        filterable
                        remote
                        reserve-keyword
                        placeholder="请选择"
                        :remote-method="defectiveMethod"
                        :loading="selLoadingL"
                        @focus='getFocus'>
                        <el-option
                            v-for="item in defective.options"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="计价方式">
                    <el-select v-model="createForm.pricingMethod" placeholder="请选择" clearable>
                        <el-option
                            v-for="item in pricingOptions"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
               
                <!-- ====================自定义==================== -->
                <el-form-item
                    v-for='(item, i) in customFiledList'
                    :key="i" 
                    :label='item.chineseName'
                    :prop="item.id"
                    v-show='hiddenRule(item)'
                >
                    <el-checkbox-group v-show="false" v-model="createForm[item.id]"></el-checkbox-group>
                    <template slot='label'>
                        <span style="color: red" v-show="requireRsterisk(item)">*</span>
                        {{ item.chineseName }}
                        <el-tooltip class="item" effect="dark" :content="item.tips" placement="top" v-show='item.tips'>
                            <span class="iconfont icon-wenhao wenhao"></span>
                        </el-tooltip>
                    </template>
                    <formLibrary :formItem='item' @formItemHandle='formItemHandle' :defaultData="defaultData" @uploadProgress='uploadProgress' ref='formLibrary'></formLibrary>
                </el-form-item>

                <!-- 工序采集数据 -->
                <el-form-item label="工序采集数据">
                    <div class='gxcjTable'>
                        <el-table :data="tableList" style="width: 100%;"
                            :header-cell-style="{background:'#fafafa',color:'#000',fontWeight:400,fontSize:'12px'}" ref='selTable'>
                            <el-table-column width="55">
                                <template slot-scope="scope">
                                    <el-button icon='el-icon-remove-outline' class='del' @click='tableDal(scope)'></el-button>
                                </template>
                            </el-table-column>
                            <el-table-column label="类型" prop="type">
                                <template slot-scope="scope">
                                    <el-select v-model="scope.row.type" filterable placeholder="请选择" @change="typeChange" ref="typeRadio">
                                        <el-option
                                        v-for="item in typeOptions"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.label">
                                        </el-option>
                                    </el-select>
                                </template>
                            </el-table-column>
                            <el-table-column  label="名称" prop="name">
                                <template slot-scope="scope">
                                    <el-input v-model='scope.row.name' :maxlength="120"></el-input>
                                </template>
                            </el-table-column>
                        </el-table>
                        <div class='addBox' >
                            <el-button class='add' @click='tableAdd' :disabled='tableAddDis'>
                                <i class='el-icon-circle-plus-outline'></i>
                                添加一行
                            </el-button>
                            <span>最多支持添加50个</span>
                        </div>
                    </div>
                </el-form-item>
            </el-form>
        </div>
        <div class="btn">
            <el-button type="primary" plain size="small" @click="handleCancel">取消
            </el-button>
            <el-button type="primary" size="small" @click="save">保存</el-button>
        </div>
    </div>
</template>

<script>
    import {
        bpOther,
        editAdd,
        processDetail
    } from '@/api/baseController'
    import {
        customFieldOther,
        fillFieldMess
    } from '@/api/customFieldController'
    import {
        distributionListData
    } from '@/api/produceController'
    import {
        loginUserMess
    } from '@/api/loginController'
    import formLibrary from '@/components/formLibrary'
    export default {
        data() {
            return {
                customFiledList: [],
                title: '创建工序',
                createForm: {
                    code: '',
                    name: '',
                    permissionsName: [],
                    rate: 1,
                    badProductName: [],
                    other: '',
                    pricingMethod:'',
                    id: '',
                    permissions: '',
                    badProduct: '',
                    permissionsDep: ''
                },
                // 表单校验规则
                rules: {
                    name: [{
                        required: true,
                        message: '工序名称必填',
                        trigger: 'blur'
                    }],
                    permissionsName: [{
                        required: true,
                        message: '报工权限必填',
                    }],
                    rate: [{
                        message: '报工数配比不能为空！',
                        required: true,
                    }],
                    badProductName: [{
                        required: true,
                        message: '不良品项列表必选',
                    }]
                },
                searchloading: false,
                disabled: false, // 是否禁用
                tableList: [], // 工序采集数据表格数据
                typeOptions: [{
                    value: 'ipt',
                    label: '文本'
                },{
                    value: 'inputNumber',
                    label: '数字'
                },{
                    value: 'datePicker',
                    label: '时间'
                },{
                    value: 'picture',
                    label: '图片'
                }],
                pricingOptions: [{
                    value: 0,
                    label: '计件'
                    }, {
                    value: 1,
                    label: '计时'
                }],
                defective: {
                    options: [],
                    num: 0
                },
                process: {
                    options: [],
                    num: 0
                },
                selLoadingL: false,
                showMsg: '创建成功',
                // 报工权限下拉
                defaultProps: {
                    children: 'listDown',
                    label: 'name'
                },
                currentKey: [],
                checkedNodes: [],
                keys: [],
                checkedKeys: [],
                distributionList: {
                    userIds: [],
                    depIds: [],
                    permissions: ''
                },
                isFirst: 0,
                defaultData: [], // 自定义字段值
                customFiledData: [],
                customState: true, // 判断单选/复选 选值是否正确
                customErrorLabel: '',// 选值错误提示语
                // 关联属性信息
                relevanceProp: [],
                userMess: {},
                uploadFlag: true,
                loading: false,
                tableAddDis: false
            }
        },
        components: {
            formLibrary
        },
        created() {},
        mounted() {
            this.showMsg = '编辑成功'
            this.loading = true
            if(this.$route.query.new) {
                this.showMsg = '编辑成功'
                this.title = '编辑工序'
                this.disabled = true
                this.checked = []
                let data = {
                    id: this.$route.query.id
                }
                this.getProcessDetail(data)
            } else {
                this.getCustomFieldOther()
            }
            var routerData = this.$route.meta.breadList
            this.$route.meta.breadList[routerData.length - 1].name = this.title
            this.getBpOther({name: ''})
        },
        methods: {
            // 接口===========================
            // 详情
            getProcessDetail(data) {
                processDetail(data).then(res => {
                    if(res.code == 200) {
                        // 表单项赋值
                        for(let i in this.createForm) {
                            if(i == 'permissionsName') {
                                let arr = res.data[i].split(',')
                                arr[arr.length - 1] == '' ? arr.pop() : ''
                                this.$set(this.createForm, i, arr)
                            } else if(i == 'badProductName') {
                                this.$set(this.createForm, i, res.data.badProduct.split(','))
                                // this.$set(this.createForm, i, res.data[i].split(','))
                            } else {
                                this.$set(this.createForm, i, res.data[i])
                            }
                        }
                        this.customFiledData = []
                        this.defaultData = []
                        
                        this.getCustomFieldOther()

                        // 不为null说明该条数据有自定义字段，将自定义字段值初始化给表单
                        if(res.data.customFieldVos != null) {
                            res.data.customFieldVos.map(item => {
                                this.$set(this.createForm, item.customFieldId, item.value)
                                this.customFiledData.push({
                                    customFieldId: item.customFieldId,
                                    value: item.value
                                })
                                this.defaultData.push({
                                    customFieldId: item.customFieldId,
                                    value: item.value
                                })
                            })
                            this.createForm.id = res.data.id
                        //     this.defaultData = this.createForm
                        }
                        if(res.data.vos != null) {
                            this.tableList = res.data.vos.map((k, i) => {
                                return {
                                    id: i,
                                    name: k.name,
                                    type: k.type
                                }
                            })
                        }
                        
                        this.getDistributionListData(1)
                    }
                })
            },
            // 自定义字段
            getCustomFieldOther() {
                customFieldOther({type: 'processes'}).then(res => {
                    this.customFiledList = []
                    this.relevanceProp = []
                    if(res.code == 200 && res.data.length > 0) {
                        // 自定义字段数据
                        res.data.map(item => {
                            item.customFieldMess.objectsValue = item.objectsValue
                            this.customFiledList.push(item.customFieldMess)
                        })
                        // 添加自定义字段的校验规则(是否必填)
                        this.customFiledList.map(item => {
                            let r = item.hidden
                            item.hidden = r.replace(/\${工序./g, 'formItemData["').replace(/\}/g, '"]').replace(/\${用户信息./g,'userFormItemData["')
                            item.id = item.id + ''
                            let obj = {}
                            if(JSON.parse(item.fieldAttribute).required && item.fieldType != 'inputNumber') {
                                obj[item.id] = [{
                                    required: true,
                                    message: item.chineseName + '不能为空'
                                }]
                            }else if(item.fieldType == 'inputNumber'){
                                const numberRule = (rule, v, callback) => {
                                    let tempValue = ''
                                    let index = String(this.createForm[item.id]).indexOf('.')
                                    tempValue = index != -1 ? String(this.createForm[item.id]).slice(0,index) : String(this.createForm[item.id])
                                    if (!this.createForm[item.id] && item.isRequired) {
                                        return callback(new Error(`${item.chineseName}不能为空！`));
                                    }else if(tempValue.length > 10){
                                        return callback(new Error('整数位最多支持10位数字'));
                                    }else if(this.createForm[item.id] != undefined && !(/^[0-9]+(.[0-9]{1,6})?$/.test(this.createForm[item.id]))){
                                        return callback(new Error('小数点后最多保留6位数字'));
                                    }else {
                                        callback()
                                    }
                                }
                                obj[item.id] = [{
                                    validator: numberRule,
                                    trigger:'blur'
                                }]
                            }
                            this.rules = Object.assign(this.rules, obj)
                            if(item.fieldType == 'relevanceProp') {
                                this.relevanceProp.push({
                                    id: item.id,
                                    objectFieldId: item.objectFieldId,
                                    type: item.correlationField
                                })
                            }
                        })
                    }
                    this.getLoginUserMess()
                })
            },
            // 用户信息
            getLoginUserMess() {
                loginUserMess({}).then(res => {
                    this.loading = false
                    if(res.code == 200) {
                        this.userMess = res.data
                    }
                })
            },
            // 关联对象-字段信息
            getFillFieldMess(data, fillRule, n) {
                fillFieldMess(data).then(res => {
                    this.defaultData = []
                    for(let key in this.createForm) {
                        if(Number(key)) {
                            this.defaultData.push({
                                customFieldId: key,
                                value: this.createForm[key]
                            })
                        }
                    }
                    if(res.code == 200) {
                        // 如果该条数据有自定义字段
                        if(res.data.customFieldVos != null) {
                            /* 
                                遍历关联对象的填充规则
                                遍历该条数据的自定义字段列表
                                将填充规则中的来源值(from) 赋给 指定控件(to)
                            */
                            fillRule.map(item => {
                                let dataArr = []
                                let f = res.data.customFieldVos.find(res => { return res.customFieldId == item.from })
                                if(f != undefined) {
                                    res.data.customFieldVos.map(k => {
                                        if(k.customFieldId == item.from) {
                                            this.createForm[item.to] = k.value
                                            this.defaultData = this.defaultData.filter(j => j.customFieldId != item.to)
                                            this.defaultData.push({
                                                customFieldId: item.to,
                                                value: k.value
                                            })
                                        }
                                    })
                                } else {
                                    this.createForm[item.to] = ''
                                    this.defaultData.map(k => {
                                        if(k.customFieldId == item.to) {
                                            let d = this.customFiledList.find(res => { return res.id == item.to })
                                            if(d.fieldType == 'picture' || d.fieldType == 'upload' || d.fieldType == 'selectChecked') {
                                                dataArr.push({
                                                    customFieldId: k.customFieldId,
                                                    value: "[]"
                                                })
                                            } else { 
                                                dataArr.push({
                                                    customFieldId: k.customFieldId,
                                                    value: ""
                                                })
                                            }
                                        } else {
                                            dataArr.push({
                                                customFieldId: k.customFieldId,
                                                value: k.value
                                            })
                                        }
                                    })
                                    this.defaultData = dataArr
                                }
                            })
                        } else {
                            fillRule.map(item => {
                                let dataArr = []
                                this.createForm[item.to] = ''
                                this.defaultData.map(k => {
                                    if(k.customFieldId == item.to) {
                                        let d = this.customFiledList.find(res => { return res.id == item.to })
                                        if(d.fieldType == 'picture' || d.fieldType == 'upload' || d.fieldType == 'selectChecked') {
                                            dataArr.push({
                                                customFieldId: k.customFieldId,
                                                value: "[]"
                                            })
                                        } else { 
                                            dataArr.push({
                                                customFieldId: k.customFieldId,
                                                value: ""
                                            })
                                        }
                                    } else {
                                        dataArr.push({
                                            customFieldId: k.customFieldId,
                                            value: k.value
                                        })
                                    }
                               })
                               this.defaultData = dataArr
                            })
                        }
                        // 关联属性赋值
                        this.relevanceProp.map(item => {
                            if(item.objectFieldId == n) {
                                this.createForm[item.id] = res.data[item.type]
                                this.defaultData = this.defaultData.filter(j => j.customFieldId != item.id)
                                this.defaultData.push({
                                    customFieldId: item.id,
                                    value: res.data[item.type]
                                })
                            }
                        })
                    }
                })
            },
            /*
                data=1 由编辑进入,为了处理报工权限的默认勾选
                permissions: 编辑进入携带的报工权限中人员id
                permissionsDep: 编辑进入携带的报工权限中部门id
            */
            getDistributionListData(data) {
                distributionListData().then(res => {
                    let keys = []
                    let permissions = []
                    let permissionsDep = []
                    if(data == 1) {
                        // 将编辑携带的数据的报工权限包含的员工id和部门id进行处理保存
                        permissions = this.createForm.permissions.split(',')
                        permissions[permissions.length - 1] == '' ? permissions.pop() : ''
                        permissions = this.removeRepeat(permissions)

                        permissionsDep = this.createForm.permissionsDep != null ? this.createForm.permissionsDep.split(',') : ''
                        permissionsDep[permissionsDep.length - 1] == '' ? permissionsDep.pop() : ''
                        permissionsDep = this.removeRepeat(permissionsDep)
                    }
                    if (res.code == 200) {
                        // 处理接口数据，node用来保存原id，对id进行处理，防止树结构渲染时key有重复导致报错
                        res.data.unshift({
                            id: -1,
                            listDown: [],
                            name: '所有人',
                            type: 'all',
                            node: -1
                        })
                        res.data.map((item, i) => {
                            item.node = item.id
                            item.id = item.type + item.id
                            // 由编辑进入，勾选默认用户(与部门同级的用户)
                            if(data == 1 && item.type == 'user' && permissions.indexOf(item.node+'') != -1) {
                                this.checked.push(item)
                            }
                            // 由编辑进入，勾选默认部门
                            if(data == 1 && item.type == 'dep' && permissionsDep.indexOf(item.node+'') != -1) {
                                this.checked.push(item)
                            }
                            item.listDown.map((k, j) => {
                                k.node = k.id
                                k.id = item.node + '' + k.id
                                // 由编辑进入，勾选默认用户(部门下的用户)
                                if(data == 1 && permissions.indexOf(k.node+'') != -1) {
                                    this.checked.push(k)
                                }
                            })
                        })
                        // serCheckedKeys()设置默认勾选项，设置接口参数中报工权限用户id和部门id的初始值
                        if(data == 1) {
                            let d = []
                            if(this.createForm.permissionsName[0] == '所有人') {
                                d = ['all-1']
                                this.distributionList.depIds = []
                                this.distributionList.userIds = []
                                this.distributionList.permissions = '-1'
                            } else {
                                this.checked.map(item => {
                                    d.push(item.id)
                                })
                                this.distributionList.depIds = this.removeRepeat(permissionsDep)
                                this.distributionList.userIds = this.removeRepeat(permissions)
                                this.distributionList.permissions = ''
                            }
                            this.$refs.roleTree.setCheckedKeys(d)
                        }
                        this.process.options = res.data

                        // 不为null说明该条数据有自定义字段，将自定义字段值初始化给表单
                        if(res.data.customFieldVos != null) {
                            res.data.customFieldVos.map(item => {
                               this.createForm[item.customFieldId] = item.value
                               this.customFiledData.push({
                                   customFieldId: item.customFieldId,
                                   value: item.value
                               })
                               this.defaultData.push({
                                   customFieldId: item.customFieldId,
                                   value: item.value
                               })
                            })
                            this.createForm.id = res.data.id
                        //     this.defaultData = this.createForm
                        }
                    }
                })
            },

            // 功能===========================
            // 不良品项下拉列表
            getBpOther(data) {
                bpOther(data).then(res => {
                    this.selLoading = false
                    if(res.code == 200) {
                        this.defective.options = res.data.map(item => {
                            return {
                                value: `${item.id}`,
                                label: item.code ? `${item.code} | ${item.name}` : `${item.name}`,
                                node: `${item.name}`
                            };
                        });
                    }
                })
            },
            // 创建编辑成功
            getEditAdd(data) {
                editAdd(data).then(res => {
                    if(res.code == 200) {
                        this.utils.showMsg(this.showMsg, 'success')
                        this.$router.push('/basicData/procedure')
                    }
                })
            },
            save() {
                this.$refs.createForm.validate((valid) => {
                    if (valid) {
                        let data = {
                            code: this.createForm.code,
                            name: this.createForm.name,
                            badProduct: this.dataProcessing(this.createForm.badProductName, this.defective.options),
                            permissions: this.distributionList.permissions,
                            rate: this.createForm.rate,
                            other: '',
                            pricingMethod: this.createForm.pricingMethod,
                            userIds: this.distributionList.userIds.length > 0 ? (this.distributionList.userIds).join(',') : '',
                            depIds:  this.distributionList.depIds.length > 0 ? (this.distributionList.depIds).join(',') : '',
                        }
                        this.createForm.id ? data.id = this.createForm.id : ''
                        let customData = []
                        for(let key in this.createForm) {
                            if(Number(key)) {
                                this.customFiledList.find(item => {
                                    if(
                                        item.id == key &&
                                        this.createForm[key] !== '' &&
                                        this.createForm[key] != '[]' &&
                                        this.createForm[key] != '{}' &&
                                        this.createForm[key] != null &&
                                        !(Array.isArray(this.createForm[key]))
                                    ) {
                                        customData.push({
                                            customFieldId: item.id,
                                            value: this.createForm[key]
                                        })
                                    }
                                    if(Array.isArray(this.createForm[key]) && this.createForm[key].length > 0) {
                                         customData.push({
                                            customFieldId: item.id,
                                            value: this.createForm[key]
                                        })
                                    }
                                })
                            }
                        }
                        data.customFieldVos = customData

                        // 关联对象后，判断单选框/复选框关联过来的值是否存在
                        this.customState = true
                        this.customErrorLabel = ''
                        this.customFiledList.map(item => {
                            // 单选框
                            if(item.fieldType == 'selectRadio') {
                                if(this.createForm[item.id] != '' && this.createForm[item.id] != undefined) {
                                    let s = JSON.parse(item.checkBox).find(res => { return this.createForm[item.id] == res.value })
                                    if(s == undefined) {
                                        this.customState = false
                                        this.customErrorLabel = '【' + item.chineseName + '】选项值不正确'
                                    }
                                }
                            }
                            // 复选框
                            if(item.fieldType == 'selectChecked') {
                                if(this.createForm[item.id] != '' && this.createForm[item.id] != undefined && this.createForm[item.id] != '[]') {
                                    JSON.parse(this.createForm[item.id]).map(k => {
                                        let s = JSON.parse(item.checkBox).find(res => { return res.value == k })
                                        if(s == undefined) {
                                            this.customState = false
                                            this.customErrorLabel = '【' + item.chineseName + '】选项值不正确'
                                        }
                                    })
                                }
                            }
                        })
                        data.vos = this.tableList.map(k => {
                            return {
                                name: k.name,
                                type: k.type,
                                value: this.vosSet(k.type)
                            }
                        })
                        // console.log(data.vos)
                        if(this.customState) {
                            if(this.uploadFlag) {
                                if(this.$refs.formLibrary != undefined) {
                                    this.$refs.formLibrary.map(k => {
                                        k.removeData('ok')
                                    })
                                }
                                // this.getProductHandle(data)
                                this.getEditAdd(data)
                            } else {
                                this.utils.showMsg('正在上传，请稍后', 'warning')
                            }
                        } else {
                            this.utils.showMsg(this.customErrorLabel, 'error')
                        }
                    } else {
                        return false;
                    }
                });
            },
            // 取消
            handleCancel() {
                if(this.$refs.formLibrary != undefined) {
                    this.$refs.formLibrary.map(k => {
                        k.removeData('cancle')
                    })
                }
                this.$router.push('/basicData/procedure')
            },
            //不良品项列表搜索
            getFocus() {
                if(this.defective.num == 0) {
                    this.defectiveMethod('')
                    this.defective.num++
                }
            },
            defectiveMethod(query) {
                this.selLoading = true;
                this.getBpOther({name: query})
            },

            // 报工权限
            //复选框和节点
            handleNodeCheck(currentKey, checked) {
                this.checked = checked.checkedNodes
                this.currentKey = currentKey
                this.distributionList.depIds = []
                this.distributionList.userIds = []
                if (currentKey.node == -1) {
                    this.createForm.permissionsName = [] //先置空
                    this.createForm.permissionsName = [...this.createForm.permissionsName, currentKey.name]
                    this.$refs.roleTree.setCheckedKeys([currentKey.id])
                    this.distributionList.permissions = '-1'
                } else {
                    let arr = []
                    this.distributionList.permissions = ''
                    let keys = checked.checkedKeys.filter(n => {
                        return n != 'all-1'
                    })
                    checked.checkedNodes.map(item => {
                        if(item.node != -1) {
                            arr = [...arr, item.name]
                            if(item.type == 'user' || item.type == null) {
                                this.distributionList.userIds = [...this.distributionList.userIds, item.node]
                            } else if(item.type == 'dep') {
                                this.distributionList.depIds = [...this.distributionList.depIds, item.node]
                            }
                        }
                    })
                    this.distributionList.userIds = this.removeRepeat(this.distributionList.userIds)
                    this.distributionList.depIds = this.removeRepeat(this.distributionList.depIds)
                    this.createForm.permissionsName = this.removeRepeat(arr)
                    this.$refs.roleTree.setCheckedKeys(this.removeRepeat(keys))
                }
            },
            //报工权限下拉数据
            treeChange(v){
                if(v) this.getDistributionListData()
            },
            // 报工权限移除tag选项
            tabRemove() {
                this.keys = []
                let ids = []
                let deps = []
                if (!this.createForm.permissionsName.length) return this.$refs.roleTree.setCheckedKeys([])
                this.checked.forEach(n => {
                    if (this.createForm.permissionsName.indexOf(n.name) != -1) {
                        this.keys = [...this.keys, n.id]
                        if(n.type == 'dep') {
                            deps = [...deps, n.node]
                        } else if(n.type == 'user' || n.type == null) {
                            ids = [...ids, n.node]
                        }
                    }
                })
                this.distributionList.depIds = this.removeRepeat(deps)
                this.distributionList.userIds = this.removeRepeat(ids)
                this.currentKey.id == 'all-1' ?
                    this.$refs.roleTree.setCheckedKeys([this.currentKey.id]) : this.$refs.roleTree
                    .setCheckedKeys(this.keys)
            },
                
            // 工序采集表格删除行
            tableDal(data) {
                this.tableList.splice(data.row.id, 1)
                this.tableList.map((item, i) => {
                    item.id = i
                })
                if(this.tableList.length < 50) {
                    this.tableAddDis = false
                }
            },
            // 工序采集表格添加行
            tableAdd() {
                this.tableList.push({
                    name: '',
                    type: '文本',
                    id: this.tableList.length
                })
                if(this.tableList.length >= 50) {
                    this.tableAddDis = true
                }
            },
            // 下拉数据处理
            dataProcessing(v, data) {
                v.forEach((item, i) => {
                    if(!Number(item)) {
                        let val = ''
                        val = data.find(k => {
                            return k.node == item
                        })
                        v[i] = val.value
                    }
                })
                return v.join(',')
            },
            // 数组去重
            removeRepeat(data) {
                let arr = Array.from(new Set(data))
                return arr
            },

            // 自定义表单项数据--表单子组件传递的值
            formItemHandle(d) {
                // 子组件的值赋给表单
                this.$set(this.createForm, d.name, d.value)
                // 操作的是关联对象
                if(d.relevanceObject != undefined) {
                    if(d.relevanceObject.id) {
                        let data = {
                            id: d.relevanceObject.id,
                            type: d.relevanceObject.businessObject
                        }
                        this.getFillFieldMess(data, d.relevanceObject.fillRule, d.name)
                    } else {
                        this.customFiledList.map(item => {
                            if(item.objectFieldId == d.name) {
                                this.createForm[item.id] = ''
                                this.defaultData.map(k => {
                                    if(k.customFieldId == item.id || k.customFieldId == d.name) {
                                        k.value = ''
                                    }
                                })
                            }
                        })
                        for(let key in this.createForm) {
                            let ind = this.defaultData.findIndex(res => {return res.customFieldId == key })
                            if(ind != -1) {
                                this.$set(this.defaultData, ind, { customFieldId: key, value: this.createForm[key] })
                            } else {
                                this.defaultData.push({
                                    customFieldId: key,
                                    value: this.createForm[key]
                                })
                            }
                        }
                    }
                }
            },
            // 自定义隐藏设置--隐藏条件触发
            hiddenRule(d) {
                let formItemData = {
                    '工序编号': this.createForm.code,
                    '工序名称': this.createForm.name,
                    '报工权限': this.distributionList.permissions,
                    '报工数配比': this.createForm.rate,
                    '不良品项列表': this.customData(this.createForm.badProductName, this.defective.options),
                }
                let userFormItemData = {
                    '账号': this.userMess.account,
                    '姓名': this.userMess.name,
                    '手机号': this.userMess.mobile,
                    '角色': this.userMess.role,
                }
                this.customFiledList.map(item => {
                    if(formItemData[item.chineseName] == undefined) {
                        if(this.createForm[item.id] != undefined) {
                            formItemData[item.chineseName] = this.createForm[item.id]
                        } else {
                            formItemData[item.chineseName] = ''
                        }
                    }
                     if(userFormItemData[item.chineseName] == undefined) {
                        if(this.createForm[item.id] != undefined) {
                            userFormItemData[item.chineseName] = this.createForm[item.id]
                        } else {
                            userFormItemData[item.chineseName] = ''
                        }
                    }
                })
                let state = null
                try {
                    state = !eval(d.hidden)
                } catch {
                    state = true
                }
                //必填项字段若是被隐藏不校验
                if (this.rules[d.id]) {
                    if(d.fieldType == 'inputNumber'){
                        if(JSON.parse(d.fieldAttribute).required){ //inputNumber 必填 && 切换触发隐藏条件
                            d.isRequired = state
                        }
                    }else{
                        this.rules[d.id][0].required = state
                    }
                }
                return state
            },
            requireRsterisk(d) {
                if (this.rules[d.id]) {
                    if(d.fieldType == 'inputNumber'){
                        return JSON.parse(d.fieldAttribute).required || false
                    }else{
                        return this.rules[d.id][0].required || false
                    }
                }
            },
            customData(v, data) {
                if(Number(v)) {
                    let id = ''
                    data.map(item => {
                        if(item.value == v) {
                            id = item.label
                        }
                    })
                    return id
                } else {
                    return v
                }
            },
            // 附件/图片上传是否完成
            uploadProgress(s) {
                this.uploadFlag = s
            },
            typeChange() {
                this.$nextTick(() => {
                    this.$refs.typeRadio.blur()
                })
            },

            vosSet(t) {
                let s = ''
                if(t == '文本') {
                    s = 'ipt'
                } else if(t == '数字') {
                    s = 'inputNumber'
                } else if(t == '时间') {
                    s = 'datePicker'
                } else {
                    s = 'picture'
                }
                return s
            }
        },
        watch: {
            createForm: {
                handler(nVal, oVal) {
                    this.hiddenRule('')
                },
                deep: true,
            }
        }
    }
</script>

<style lang="less" scoped>
    .createProcedure {
        overflow: auto;
        .title {
            font-size: 22px;
            padding: 20px 0 5px 20px;
        }
        .formArea {
            width: 420px;
            padding: 0px 0px 0px 7px;
            .gxcjTable {
                width: 500px;
                border: 1px solid #eee;
                .del {
                    color: red !important;
                    border: none;
                    font-size: 18px;
                    padding: 0;
                    display: flex;
                    justify-content: center;
                    width: 100%;
                    background: transparent;
                }
                .addBox {
                    color: #02b980;
                    display: flex;
                    align-items: center;
                    margin: 16px;
                    cursor: pointer;
                    .add {
                        color: #02b980;
                        border: none;
                        background: transparent;
                        font-size: 16px;
                        padding: 0;
                        font-size: 14px;
                    }
                    span {
                        color: rgb(155, 155, 155);
                        margin-left: 10px;
                    }
                }
            }
        }

        .productTask {
            margin: 60px 40px 0px;

            .operate {
                span {
                    font-size: 12px;
                    float: left;
                }

                .operate_btn {
                    float: right;
                }

                margin-bottom: 10px;
            }

            .btn {
                margin: 26px 0;
                text-align: center;

                /deep/ .el-button--primary span {
                    margin-left: 0;
                }
            }
        }

        .btn {
            margin: 25px 0 20px 60px;
            text-align: center;
            display: inline-block;
            width: 420px;

            /deep/ .is-plain {
                margin-right: 10px;
            }

            /deep/ .el-button--primary {
                width: 130px;
                span {
                    margin-left: 0;
                }
            }
        }

        /deep/ .el-input-number--mini {
            width: 100%;
        }

        /deep/ .el-select {
            width: 100%;
        }

        /deep/ .el-range-editor--mini.el-input__inner {
            width: 100%;
        }

        /deep/ .el-table__empty-text {
            .el-empty {
                padding: 0;
            }
        }

        /deep/ .el-empty__image {
            height: 60px;
        }

        /deep/ .el-empty__image svg {
            vertical-align: text-top;
        }

        /deep/ .el-empty__description {
            margin-top: 10px;
        }

        /deep/ .el-range-editor.el-input__inner {
            width: 100%;
        }

        /deep/ .el-range-editor .el-range-input {
            flex: 7;
        }

        /deep/ .el-date-editor .el-range-separator {
            flex: 1;
        }

        /deep/ .el-input-group__append {
            background-color: #02b980;
            color: white;
        }
        /deep/ .el-input__inner {
            text-align: left;
        }
        /deep/ .el-table__body > tbody > tr:nth-last-child(1) {
            td {
                border-bottom: none;
            }
        }
        /deep/ .el-tag.el-tag--info {
            transition: all 0.2s;
        }
        /deep/ .el-table__body-wrapper {
            max-height: 391px;
            overflow-y: auto;
            .el-input {
                width: 80%;
            }
        }
    }
</style>